from haystack import indexes
from course.models import CourseModel


class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    id = indexes.IntegerField(model_attr='id')
    c_name = indexes.CharField(model_attr='c_name')
    content = indexes.CharField(model_attr="content")
    teacher = indexes.CharField(model_attr="teacher")
    cover = indexes.CharField(model_attr='cover')
    price = indexes.DecimalField(model_attr='price')
    discount = indexes.DecimalField(model_attr='discount')

    def get_model(self):
        return CourseModel

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def prepare_text(self, obj):
        return f"{obj.c_name}{obj.content}"
